package cumt.kydd.authoringplatform.controller;


import com.alibaba.fastjson.JSONObject;
import cumt.kydd.authoringplatform.bean.History;
import cumt.kydd.authoringplatform.service.SendHistoryService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

@Controller
public class SendHistoryController {
    @Resource
    private SendHistoryService sendHistoryService;


    public List<Integer> getUsersHistoryID()
    {
        List<Integer> result = new LinkedList<>();
        result = sendHistoryService.getUsersHistoryID();

        return result;
    }

    public List<Integer> getUsersHistoryTrashID()
    {
        List<Integer> result = new LinkedList<>();
        result = sendHistoryService.getUsersHistoryTrashID();

        return result;
    }

    public List<Integer> getUsersHistoryFavorID()
    {
        List<Integer> result = new LinkedList<>();
        result = sendHistoryService.getUsersHistoryFavorID();

        return result;
    }

    @ResponseBody
    @PostMapping("history")
    public String SendHistory(@RequestParam(required = false) List<History> history){

        List<Integer> IDList = getUsersHistoryID();
//        System.out.println("service:"+IDList);
        history = new ArrayList<History>();
        history = sendHistoryService.sendHistory(history);
//        System.out.println(history);
        JSONObject result = new JSONObject();
        int total=0;
        for (int i = 0; i < history.size(); i++) {
//            System.out.println("(Controller里面)是否已经被删除？"+history.get(i).getIsDelete());
            total++;
        }
        result.put("his",history);
        result.put("total",total);
        return result.toJSONString();
    }

    @ResponseBody
    @PostMapping("trash")
    public String SendHistoryTrash(@RequestParam(required = false) List<History> history){

        List<Integer> IDList = getUsersHistoryTrashID();
//        System.out.println("service:"+IDList);
        history = new ArrayList<History>();
        history = sendHistoryService.sendHistoryTrash(history);
        System.out.println(history);
        JSONObject result = new JSONObject();
        int total=0;
        for (int i = 0; i < history.size(); i++) {
//            System.out.println("(Controller里面)是否已经被删除(应该是个1)？"+history.get(i).getIsDelete());
            total++;
        }
        result.put("his",history);
        result.put("total",total);
        return result.toJSONString();
    }

    @ResponseBody
    @PostMapping("history/showFavor")
    public String SendHistoryFavor(@RequestParam(required = false) List<History> history){

        List<Integer> IDList = getUsersHistoryFavorID();
        history = new ArrayList<History>();
        history = sendHistoryService.sendHistoryFavor(history);
        JSONObject result = new JSONObject();
        int total=0;
        for (int i = 0; i < history.size(); i++) {
//            System.out.println("(Controller里面)是否已经被收藏？"+history.get(i).getIsFavor());
            total++;
        }
        result.put("his",history);
        result.put("total",total);
        return result.toJSONString();
    }
}
